<?php
namespace app\jobmanagement\service;

use app\constdir\ApiConst;
use think\Exception;
use app\model\User as mUser;
use app\lib\Dingtalk as DingtalkLib;
use think\Db;
use think\Model;

/**
 * Class Product
 * @package app\jobmanagement\service
 */
class Super extends Model
{
    private $mUser;
    private $dd_user_id;
    protected $DingtalkLib;
    public function __construct(){
        parent::__construct();
        $this->mUser = new mUser();
        $this->DingtalkLib = new DingtalkLib();
    }


    /**
     * @introduction:根据钉钉ID进行各项检验并添加管理员
     * @param $user_id_all
     * @param $dept_id
     * @return array|bool
     * @throws \think\exception\PDOException
     * @author:cxh
     * @date: 2020/6/10 9:40
     */
    public function adminSave($user_id_all , $dept_id = 0 , $level = 1){
        $sCommon = new Common();
        //启动事务
        Db::startTrans();
        try {
            //查看数据库是否存在改人员
            foreach ($user_id_all as $item) {
                //监测数据库中是否存在该用户
                $userInfo = $sCommon->getUserInfo($item);
                if(empty($userInfo)){ //不存在新增
                    $user_info = $this->DingtalkLib->getUserInfoByDDUserId($item);
                    $data = [
                        'dd_user_id' => $item,
                        'name' => $user_info['name'],
                        'avatar' => $user_info['avatar'],
                        'level' => $level,
                        'dept_id' => $dept_id
                    ];
                    $user_id = $this->mUser->insertGetId($data);

                }else{//存在的修改
                    $data = [
                        'level' => $level,
                        'dept_id' => $dept_id
                    ];
                    $user_id = $this->mUser
                        ->where('dd_user_id',$item)
                        ->update($data);
                }
            }

            //提交事务
            Db::commit();
            return true;
        }catch (\Exception $e){
            //事务回滚
            Db::rollback();
            return false;
        }


    }


    /**
     * @introduction:根据USER_ID删除对应管理员
     * @param $param
     * @return array|bool
     * @throws \think\exception\PDOException
     * @author:cxh
     * @date: 2020/6/10 9:39
     */
    public function deleteAdmin($param){
        $mUser = new mUser();
        $user = $mUser
            ->where(['dd_user_id' => $param['id']])
            ->find();
        if (empty($user)) {
            return false;
        }
        // 删除管理员
        Db::startTrans();
        try {
            $data = $mUser
                ->where(['dd_user_id' => $param['id']])
                ->update(['level' => 3, 'dept_id' => 0]);
            //提交事务
            if ($data) {
                Db::commit();
                return true;
            } else {
                Db::rollback();
                return false;
            }
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }

    }


    /**
     * @introduction:获取阶梯计费审核员列表
     * @param $param
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/7/28 16:15
     */
    public function ladderAuditList($param){

        $mUser = new mUser();
        $list = $mUser
            ->where('level', $param['level'])
            ->select();

        if (empty($list)) {
            return [];
        } else {
            $plant = config('dic.plant_area');
            foreach ($list as $key => $item) {
                $list[$key]['dept_name'] = !empty($plant[$item['dept_id']]) ? $plant[$item['dept_id']] : '未知';
            }
            return $list;
        }
    }


}





